/**
 * @name 数据表stu_h1802操作对象
 * @author 杨标
 * @version 2.2
 * @requires BaseService
 * @class Stu_h1802Service
 * @description 2.0版本 增同Promise，实现继承 添加分页
 */

const BaseService = require("./BaseService");
const Stu_H1802 = require("../model/Stu_h1802");
const ObjectHelper = require("../utils/ObjectHelper");
const PageList = require("../model/PageList");


class Stu_h1802Service extends BaseService {
    constructor() {
        super("stu_h1802");    //实现继承以后，一定要调用super
    }
    /**
     * @name 根据学号与QQ号进行登陆检测
     * @param {Object} paramSql  学号与QQ号
     * @returns {Promise} 异步操作
     */
    checkLogin({ s_id, s_qq }) {
        return new Promise((resolve, reject) => {
            let conn = super.getConn();
            let strSql = `select * from ${this.tableName} where s_id=? and s_qq=?`;
            conn.query(strSql, [s_id, s_qq], (err, result) => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(result);
                }
            });
            conn.end();
        })
    }
    /**
     * @name 获取所有信息
     * @returns {Promise} 异步操作
     */
    getAllList() {
        return new Promise((resolve, reject) => {
            let conn = super.getConn();
            conn.query(`select * from ${this.tableName}`, [], (err, result) => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(result);
                }
            });
            conn.end();
        });
    }
    /**
     * @name 新增学生信息
     * @param {Stu_H1802} model 
     * @returns {Promise} 返回操作结果
     */
    addStudent(model) {
        return new Promise((resolve, reject) => {
            let values = Object.values(model);
            let conn = super.getConn();
            let insertSql = super.createInsertSql(model);
            conn.query(insertSql, values, (err, result) => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(result);
                }
                conn.end();
            });
        })
    }
    /**
     * @name 根据参数查询分页信息
     * @param {*} paramSql 要查询的参数
     * @param {Number} pageIndex 分页的页码
     * @returns {Promise} 返回查询以后的Promise 
     */
    queryByList({ s_id, s_name, s_sex }, pageIndex) {
        return new Promise((resolve, reject) => {
            let conn = super.getConn();
            let strSql = `select * from ${this.tableName} where 1=1 `;                    //查询的Sql
            let strCountSql = `select count(*)  'sumCount' from ${this.tableName} where 1=1 `;       //计数的Sql
            let paramSql = "";       //查询参数                                             
            //如果一旦涉及到条件拼接，那么，则需要使用一种固定的格式
            if (!ObjectHelper.isNullOrUndefined(s_id)) {
                paramSql += ` and s_id like "%${s_id}%" `;
            }
            if (!ObjectHelper.isNullOrUndefined(s_name)) {
                paramSql += ` and s_name like "%${s_name}%" `;
            }
            if (!ObjectHelper.isNullOrUndefined(s_sex)) {
                paramSql += ` and s_sex ="${s_sex}" `;
            }
            strSql += paramSql + " limit ?,? "              //拼接分页的Sql
            strCountSql += paramSql;                      //拼接计数的Sql
            conn.query(strSql + ";" + strCountSql, [(pageIndex - 1) * 10, 10], (err, result) => {
                if (err) {
                    reject(err);
                }
                else {
                    // resolve(result);
                    //这一个地方的result是一个分页信息和总条数的结果
                    //现在，我们要根据这个结果，来计算出一共分了多少页，并且把这个结果返回到页面去
                    let sumCount = result[1][0].sumCount;
                    let pageList = new PageList(pageIndex, sumCount, result[0]);
                    resolve(pageList);    //返回的一个分页信息的对象
                }
                conn.end();
            });
        });
    }

    /**
     * @name 根据学号删除记录
     * @param {String} s_id 要根据删除的学号 
     * @returns {Promise} 返回数据库操作结果Promise
     */
    deleteById(s_id) {
        return new Promise((resolve, reject) => {
            let conn = super.getConn();
            let deleteSql = `delete from ${this.tableName} where s_id=? `;
            conn.query(deleteSql, [s_id], (err, result) => {
                if (err) {
                    reject(err);
                }
                else {
                    // let flag=false;         //用来表示成功或失败
                    // if(result.affectedRows>0){
                    //     //成功
                    //     flag=true;
                    // }
                    // else{
                    //     //失败
                    //     flag=false;    
                    // }
                    let flag = result.affectedRows > 0 ? true : false;
                    resolve(flag);
                }
                conn.end();
            });
        })
    }

    /**
     * @name 批量删除
     * @param {Array} s_idArr 要批量删除的学号的数组 
     * @returns {Promise}  返回执行数据库以后的操作
     */
    deleteByChecked(s_idArr) {
        return new Promise((resolve, reject) => {
            let conn=super.getConn();
            let deleteSql=`delete from ${this.tableName} where s_id in (${new Array(s_idArr.length).fill("?").toString()})`;
            conn.query(deleteSql,s_idArr,(err,result)=>{
                if(err){
                    reject(err);
                }
                else{
                    let flag=result.affectedRows>0?true:false;
                    resolve(flag);
                }
            });
            conn.end();
        });
    }
    /**
     * @name 根据学号得到学生信息
     * @param {String} s_id 学号 
     * @returns {Promise}  返回执行数据库以后的操作 
     */
    findByS_id(s_id){
        return new Promise((resolve,reject)=>{
            let conn=super.getConn();
            let strSql=`select * from ${this.tableName} where s_id=? `;
            conn.query(strSql,[s_id],(err,result)=>{
                if(err){
                    reject(err);
                }
                else{
                    resolve(result);
                }
                conn.end();
            });
        });
    }

    /**
     * @name 修改学生信息
     * @param {Object} model 要修改的对象 
     * @returns {Promise}  返回执行数据库以后的操作
     */
    editStudent(model){
        return new Promise((resolve,reject)=>{
            let {s_name,s_sex,s_age,s_qq,s_school,s_major,s_education,s_id}=model;
            let updateSql=`update ${this.tableName} set s_name=?,s_sex=?,s_age=?,s_qq=?,s_school=?,s_major=?,s_education=? where s_id=?`;
            let conn=super.getConn();
            conn.query(updateSql,[s_name,s_sex,s_age,s_qq,s_school,s_major,s_education,s_id],(err,result)=>{
                if(err){
                    reject(err);
                }
                else{
                    let flag=result.affectedRows>0?true:false;
                    resolve(flag);
                }
                conn.end();
            });
        });
    }
    
}


module.exports = Stu_h1802Service;